package controllers;

import java.util.ArrayList;
import java.util.List;

import models.Comment;
import models.Draft;
import models.Entities;
import models.Idea;
import models.Organization;
import models.Topic;
import models.User;
import play.data.validation.Required;
import play.mvc.Controller;

/**
 * Draft controller links between models and views
 * 
 * @author Heba Khattab
 * 
 */

public class DraftController extends Controller {
	/**
	 * Opens a page for a form that contains all the atributes needed to create
	 * a draft
	 * 
	 * @author Heba Khattab.
	 */

	public static void createDraft() {
		render();

	}

	/**
	 * Gets paramters entered by user in create draft to add a new draft to
	 * system and then renders the created draft to be used after that in edit,
	 * publish and delete
	 * 
	 * @author Heba Khattab
	 */
	public static void showDraft() {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		Draft draft = new Draft(params.get("subject"), params.get("Body"), user);
		draft.save();
		showOpenedDraft(draft.id);
	}

	/**
	 * Gives a list of all drafts of a user
	 * 
	 * @author Heba Khattab
	 */

	public static void allDrafts() {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		List drafts = user.draftOfUser;
		render(drafts);
	}

	/**
	 * Takes the id of a draft and opens it to show it's details where you can
	 * edit, publish or delete
	 * 
	 * @param draftId
	 *            id of a draft
	 * 
	 * @author Heba Khattab
	 */
	public static void showOpenedDraft(Long draftId) {
		Draft draft = Draft.findById(draftId);
		render(draft);
	}

	/**
	 * Takes id of a draft that the user edited and save it then show the draft
	 * with new details
	 * 
	 * @param draftId
	 *            id of draft
	 * 
	 * @author Heba Khattab
	 */
	public static void editedDraft(Long draftId) {
		Draft draft = Draft.findById(draftId);
		draft.subject = params.get("editsubject");
		draft.body = params.get("editbody");
		draft.save();
		showOpenedDraft(draftId);
	}

	/**
	 * Takes id of a draft that the user wants to edit and open page to enter
	 * the draft new details
	 * 
	 * @param draftId
	 *            id of draft
	 * @author Heba Khattab
	 */

	public static void editDraft(Long draftId) {
		Draft draft = Draft.findById(draftId);
		render(draft);
	}

	/**
	 * Takes id of a draft that the user wants to delete and delete it then show
	 * a message that draft was deleted succefully
	 * 
	 * @param draftId
	 *            id of draft
	 * 
	 * @author Heba Khattab
	 * 
	 */

	public static void deleteDraft(Long draftId) {
		Draft draft = Draft.findById(draftId);
		draft.deletingDraft(draft);
		render();
	}

	/**
	 * Takes as an input the id of draft that will be published as idea and gets
	 * all entities that the user can publish to
	 * 
	 * @param draftId
	 *            id of draft that will be published
	 * 
	 * @author Heba Khattab
	 */

	public static void allEntOfUserIdea(Long draftId) {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		List<Organization> allOrganizations = user.organization;
		List<Entities> allEntities = new ArrayList();
		for (int counter = 0; counter < allOrganizations.size(); counter++) {
			Organization organization = allOrganizations.get(counter);
			for (int secondCounter = 0; secondCounter < organization.allEntities
					.size(); secondCounter++) {
				Entities entity = organization.allEntities.get(secondCounter);
				allEntities.add(entity);
			}
		}
		render(allEntities, draftId);

	}

	/**
	 * Takes id of a draft that the user wants to publish and entity id that
	 * will be published to, and renders the draft being published and the
	 * entity id
	 * 
	 * @param entityId
	 *            id of entity
	 * @param draftId
	 *            id of draft
	 * @author Heba Khattab
	 * 
	 */

	public static void draftIdeaEnt(Long entityId, Long draftId) {
		Draft draft = Draft.findById(draftId);
		render(entityId, draft);
	}

	/**
	 * takes id of draft that will be published as idea and renders this draft
	 * and the topic that the idea will be published to
	 * 
	 * @param topicId
	 *            id of topic that draft will be published to
	 * @param draftId
	 *            id of draft being published
	 * @author Heba Khattab
	 */
	public static void draftIdeaTopic(Long topicId, Long draftId) {
		Draft draft = Draft.findById(draftId);
		render(topicId, draft);
	}

	/**
	 * Takes id of draft that will published, gets user id, a list of all
	 * organizations that the user created and joined then creates a new list
	 * that will be filled with all the entities in all user's organizations
	 * then renders this list and the draft id
	 * 
	 * @param draftId
	 *            draft that will be published
	 * 
	 * @author Heba Khattab
	 */
	public static void allEntities(Long draftId) {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		List<Organization> allOrganizations = user.organization;
		List<Entities> allEntities = new ArrayList();
		for (int counter = 0; counter < allOrganizations.size(); counter++) {
			Organization organization = allOrganizations.get(counter);
			for (int secondCounter = 0; secondCounter < organization.allEntities
					.size(); secondCounter++) {
				Entities entity = organization.allEntities.get(secondCounter);
				allEntities.add(entity);
			}
		}
		render(allEntities, draftId);
	}

	/**
	 * Takes the id of the draft that will be published and gets all topics in
	 * all organizations that user created and joined then renders this list and
	 * the draft id
	 * 
	 * @param draftId
	 *            id of draft that will be published
	 * 
	 * @author Heba Khattab
	 */
	public static void allTopics(Long draftId) {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		List<Organization> allOrganizations = user.organization;
		List<Entities> allEntities = new ArrayList();
		List<Topic> allTopics = new ArrayList();
		for (int counter = 0; counter < allOrganizations.size(); counter++) {
			Organization organization = allOrganizations.get(counter);
			for (int secondCounter = 0; secondCounter < organization.allEntities.size(); secondCounter++) {
				Entities entity = organization.allEntities.get(secondCounter);
				allEntities.add(entity);
				for (int m = 0; m < entity.topics.size(); m++) {
					Topic topic = entity.topics.get(m);
					allTopics.add(topic);
				}
			}

		}
		render(allTopics, draftId);
	}

	/**
	 * Takes an input organzation id that the draft is published to and id of
	 * draft being published and saves draft as idea to an organization
	 * 
	 * @param OrganizationId
	 *            Organization posting to
	 * @param draftId
	 *            draft being posted
	 * 
	 * @author Heba Khattab
	 */
	public static void draftIdeaOrg(Long OrganizationId, Long draftId) {
		Draft draft = Draft.findById(draftId);
		render(OrganizationId, draft);
	}

	/**
	 * takes paramters from posting topic page and create a new draft then show
	 * it to user
	 * 
	 * @author Heba Khattab
	 */

	public static void saveTopicAsDraft() {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		String title = params.get("topic.title");
		String description = params.get("topic.body");
		Draft draft = new Draft(title, description, user);
		draft.save();
		showOpenedDraft(draft.id);
	}

	/**
	 * takes paramters from posting idea page and create a new draft then show
	 * it to user
	 * 
	 * @author Heba Khattab
	 */

	public static void saveIdeaAsDraft() {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		String title = params.get("Title");
		String body = params.get("Body");
		Draft draft = new Draft(title, body, user);
		draft.save();
		showOpenedDraft(draft.id);
	}
}
